<?php

namespace app\admin\services;

use think\App;
use think\Auth;
use think\Service;
use think\facade\Session;
use think\facade\Request;
use app\admin\model\AuthgroupModel;
use app\admin\model\AuthruleModel;
use app\admin\model\OperationModel;

class AuthruleService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        global $authstate;
        $username = Session::get('username', 'admin');
        $uid = Session::get('uid', 'admin');
        if (!$username) {
            $this->redirect('admin/inout/login');
        }
        $pathinfo = '/'.Request::pathinfo();
        $auth = new Auth();
        $userauth = $auth->check($pathinfo, $uid);
        if (!$userauth) {
            $authstate = $this->_getResponse("AUTH_EORRE");
        }
        parent::__construct($request, $app);
        $this->AuthgroupModel = new AuthgroupModel;
        $this->AuthruleModel = new AuthruleModel;
        $this->OperationModel = new OperationModel;
    }


    /**
     * @param 管理权限列表
     */
    public function Listont(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['names'])){
            $where = [['title','like','%'.$post['names'].'%'],['status','eq',1]];
        }else{
            $where=[['status','eq',1]];
        }
        $rulst = $this->AuthruleModel->listction($where,$page,$limit);
        $counts = $this->AuthruleModel->county($where);
        $array=[];
        foreach ($rulst as $value){
            $array[] = $value;
        }
        if(empty($rulst)){
            $data['rel'] = false;
            $data['msg'] = "暂无数据";
        }else{
            $data['rel'] = true;
            $data['msg'] = "获取成功";
        }
        $data['list'] = $array;
        $data['count'] = $counts;
        return $data;
    }


    /**
     * @param 获取权限
     */
    public function listonts(){
        $where = [['status','eq',1]];
        $data = $this->AuthruleModel->authlist($where);
        return $data;
    }


    /**
     * @param 权限上级位置
     */
    public function Navcat(){
        $where = [['navlevel','eq',1]];
        $data = $this->AuthruleModel->authlist($where);
        return $data;
    }


    /**
     * @param 添加权限
     */
    public function addauth(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(!$post['title']||!$post['name'] || !$post['navlevel'] || !$post['pid'] || !$post['levt']){
            return $this->_getResponse("PARAM_EORRE");
        }
        $funname = $post['name'];
        $where = [['name','eq',$funname]];
        $weiyi = $this->AuthruleModel->functionname($where); //验证方法名唯一
        if($weiyi){
            return $this->_getResponse("NAME_ERROR");
        }
        if($post['levt']==2){
            $data['navlevel'] = 0;
        }else{
            if(!empty($post['icon']) && $post['navlevel']=='new'){
                $data['icon'] = empty($post['icon'])?$post['icon']=="":$post['icon'];
                $data['navlevel'] = 1;
            }else{
                $data['navlevel'] = $post['navlevel']=='new'?$post['navlevel']==1:$post['navlevel'];
            }
        }
        $data['name'] = $funname;
        $data['title'] = $post['title'];
        $data['pid'] = $post['pid'];
        $data['sort'] = 1;
        $data['level'] = $post['levt'];
        $data['style'] = $post['style'];
        $rulst = $this->AuthruleModel->adddata($data);
        if(!$rulst){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("USER_ADD");
    }


    /**
     * @param 修改权限
     */
    public function editdata(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(!$post['title']||!$post['name'] || !$post['navlevel'] || !$post['pid'] || !$post['levt']){
            return $this->_getResponse("PARAM_EORRE");
        }
        $id = $post['authid'];
        $funname = $post['name'];
        $where = [['name','eq',$funname],['id','neq',$id]];
        $weiyi = $this->AuthruleModel->functionname($where); //验证方法名唯一
        if($weiyi){
            return $this->_getResponse("NAME_ERROR");
        }
        if($post['levt']==2){
            $data['navlevel'] = 0;
        }else{
            if(!empty($post['icon']) && $post['navlevel']=='new'){
                $data['style'] = 0;
                $data['icon'] = empty($post['icon'])?$post['icon']=="":$post['icon'];
                $data['navlevel'] = 1;
            }else{
                $data['navlevel'] = $post['navlevel']=='new'?$post['navlevel']==1:$post['navlevel'];
            }
        }
        $data['name'] = $funname;
        $data['title'] = $post['title'];
        $data['pid'] = $post['pid'];
        $data['sort'] = 1;
        $data['level'] = $post['levt'];
        if(empty($post['icon']) && $post['navlevel']!="new"){
            $data['icon'] = $post['icon'];
            $data['style'] = 1;
        }
        $rulst = $this->AuthruleModel->editdata($id,$data);
        if(!$rulst){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("EDITAUTH_ERROR");
    }


    /**
     * @param 删除权限
     */
    public function deldata(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(!$post['id']){
            return $this->_getResponse("PARAM_EORRE");
        }
        $id = $post['id'];
        $rulest = $this->AuthruleModel->deldata($id);
        if(!$rulest){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("DELETE_DATE");
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "ROLE_ERROR" => ["status" => 7, "msg" => "该角色已经存在", "data" => $data],
            "NAME_ERROR" => ["status" => 8, "msg" => "该方法名已存在", "data" => $data],
            "EDITAUTH_ERROR" => ["status" => 0, "msg" => "修改成功", "data" => $data]
        ];
        return $maps[$key];
    }

}